home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 684 / 684.xpi / chrome / fireftp.jar / content / js / etc / diff.js < prev    next >
Text File  |  2007-09-22  |  8KB  |  217 lines

  1. function diff(recursive, localParent, remoteParent, last) {
  2.   if (!gFtp.isConnected || (!localParent && !isReady())) {
  3.     return;
  4.   }
  5.  
  6.   if (!localParent) {
  7.     gMissingRemoteFiles = new Array();
  8.     gMissingLocalFiles  = new Array();
  9.     gDifferentFiles     = new Array();
  10.     gNewerFiles         = new Array();
  11.     gOlderFiles         = new Array();
  12.   }
  13.  
  14.   var localFiles      = new Array();
  15.   var remoteFiles     = new Array();
  16.  
  17.   if (localParent) {
  18.     try {
  19.       var dir     = localFile.init(localParent);
  20.       var innerEx = gFireFTPUtils.getFileList(dir, new wrapperClass(localFiles));
  21.  
  22.       if (innerEx) {
  23.         throw innerEx;
  24.       }
  25.     } catch (ex) {
  26.       debug(ex);
  27.       return;                                            // skip this directory
  28.     }
  29.  
  30.     for (var x = 0; x < gFtp.listData.length; ++x) {
  31.       remoteFiles.push(gFtp.listData[x]);
  32.     }
  33.   } else {
  34.     for (var x = 0; x < localTree.data.length; ++x) {
  35.       localFiles.push(localTree.data[x]);
  36.     }
  37.     for (var x = 0; x < remoteTree.data.length; ++x) {
  38.       remoteFiles.push(remoteTree.data[x]);
  39.     }
  40.   }
  41.  
  42.   if (recursive) {
  43.     localFiles.sort(compareName).reverse();
  44.   }
  45.  
  46.   if (recursive && !localParent) {
  47.     gFtp.beginCmdBatch();
  48.   }
  49.  
  50.   var anyRecursion = false;
  51.   var firstFolder  = true;
  52.  
  53.   for (var x = 0; x < localFiles.length; ++x) {
  54.     var found = false;
  55.  
  56.     for (var y = 0; y < remoteFiles.length; ++y) {
  57.       if (localFiles[x].leafName == remoteFiles[y].leafName) {
  58.         found = true;
  59.         var aLocalFile = localFiles[x];
  60.         var remoteFile = remoteFiles[y];
  61.         remoteFiles.splice(y, 1);
  62.  
  63.         if (aLocalFile.fileSize != remoteFile.fileSize && !aLocalFile.isDirectory() && !remoteFile.isDirectory()) {
  64.           gDifferentFiles.push({ localFile: aLocalFile, remoteFile: remoteFile, action: "nothing",
  65.                                  reason: aLocalFile.fileSize > remoteFile.fileSize
  66.                                        ? gStrbundle.getString("diffBigger") : gStrbundle.getString("diffSmaller"),
  67.                                  localParent: localParent, remoteParent: remoteParent,
  68.                                  sortPath: aLocalFile.path.replace(gSlash == "/" ? /\x2f/g : /\x5c/g, "\x01").toLowerCase() });
  69.           break;
  70.         }
  71.  
  72.         if (aLocalFile.isDirectory() != remoteFile.isDirectory()) {
  73.           gDifferentFiles.push({ localFile: aLocalFile, remoteFile: remoteFile, reason: gStrbundle.getString("diffTypeMismatch"), action: "nothing",
  74.                                  localParent: localParent, remoteParent: remoteParent,
  75.                                  sortPath: aLocalFile.path.replace(gSlash == "/" ? /\x2f/g : /\x5c/g, "\x01").toLowerCase() });
  76.           break;
  77.         }
  78.  
  79.         if (gFtp.timestampsMode && aLocalFile.lastModifiedTime > remoteFile.lastModifiedTime && !aLocalFile.isDirectory() && !remoteFile.isDirectory()) {
  80.           gNewerFiles.push({ localFile: aLocalFile, remoteFile: remoteFile, action: "upload",
  81.                                  localParent: localParent, remoteParent: remoteParent,
  82.                                  sortPath: aLocalFile.path.replace(gSlash == "/" ? /\x2f/g : /\x5c/g, "\x01").toLowerCase() });
  83.           break;
  84.         }
  85.  
  86.         if (gFtp.timestampsMode && aLocalFile.lastModifiedTime < remoteFile.lastModifiedTime && !aLocalFile.isDirectory() && !remoteFile.isDirectory()) {
  87.           gOlderFiles.push({ localFile: aLocalFile, remoteFile: remoteFile, action: "download",
  88.                                  localParent: localParent, remoteParent: remoteParent,
  89.                                  sortPath: aLocalFile.path.replace(gSlash == "/" ? /\x2f/g : /\x5c/g, "\x01").toLowerCase() });
  90.           break;
  91.         }
  92.  
  93.         if (recursive && aLocalFile.isDirectory() && remoteFile.isDirectory()) {
  94.           makeDiffCallback(aLocalFile, remoteFile, (firstFolder && !localParent) || last);
  95.           last         = false;
  96.           anyRecursion = true;
  97.           firstFolder  = false;
  98.           break;
  99.         }
  100.  
  101.         break;
  102.       }
  103.     }
  104.  
  105.     if (!found) {
  106.       gMissingRemoteFiles.push({ file: localFiles[x], action: "upload", localParent: localParent, remoteParent: remoteParent,
  107.                                  sortPath: localFiles[x].path.replace(gSlash == "/" ? /\x2f/g : /\x5c/g, "\x01").toLowerCase() });
  108.     }
  109.   }
  110.  
  111.   if (recursive && !localParent) {
  112.     gFtp.endCmdBatch();
  113.   }
  114.  
  115.   for (var x = 0; x < remoteFiles.length; ++x) {
  116.     gMissingLocalFiles.push({ file: remoteFiles[x], action: "download", localParent: localParent, remoteParent: remoteParent,
  117.                               sortPath: remoteFiles[x].path.replace(/\x2f/g, "\x01").toLowerCase() });
  118.   }
  119.  
  120.   if (!recursive || last || (!anyRecursion && !localParent)) {
  121.     finalDiffCallback(recursive);
  122.   }
  123. }
  124.  
  125. function makeDiffCallback(aLocalFile, remoteFile, last) {
  126.   var func = function() {
  127.     diff(true, aLocalFile.path, remoteFile.path, last);
  128.   };
  129.  
  130.   gFtp.list(remoteFile.path, func, true, true);
  131. }
  132.  
  133. function finalDiffCallback(recursive) {
  134.   if (gMissingLocalFiles.length == 0 && gMissingRemoteFiles.length == 0 && gDifferentFiles.length == 0
  135.    && gNewerFiles.length == 0 && gOlderFiles.length == 0) {
  136.     doAlert(gStrbundle.getString("diffSame"));
  137.     return;
  138.   }
  139.  
  140.   if (recursive) {
  141.     gMissingLocalFiles.sort(directorySort);
  142.     gMissingRemoteFiles.sort(directorySort);
  143.     gDifferentFiles.sort(directorySort);
  144.     gNewerFiles.sort(directorySort);
  145.     gOlderFiles.sort(directorySort);
  146.   }
  147.  
  148.   var result = { value : false };
  149.   window.openDialog("chrome://fireftp/content/diff.xul", "diff", "chrome,modal,dialog,resizable,centerscreen",
  150.                     gMissingLocalFiles, gMissingRemoteFiles, gDifferentFiles, gNewerFiles, gOlderFiles, result, recursive);
  151.  
  152.   if (result.value) {
  153.     var transferObj           = new transfer();
  154.     transferObj.prompt        = false;
  155.     transferObj.localRefresh  = gLocalPath.value;
  156.     transferObj.remoteRefresh = gRemotePath.value;
  157.  
  158.     for (var x = 0; x < gMissingLocalFiles.length;  ++x) {
  159.       if (gMissingLocalFiles[x].action  != "nothing") {
  160.         transferObj.start(gMissingLocalFiles[x].action  == "download", gMissingLocalFiles[x].file,
  161.                           gMissingLocalFiles[x].localParent, gMissingLocalFiles[x].remoteParent);
  162.       }
  163.  
  164.       if (transferObj.cancel) {
  165.         return;
  166.       }
  167.     }
  168.  
  169.     for (var x = 0; x < gMissingRemoteFiles.length; ++x) {
  170.       if (gMissingRemoteFiles[x].action != "nothing") {
  171.         transferObj.start(gMissingRemoteFiles[x].action == "download", gMissingRemoteFiles[x].file,
  172.                           gMissingRemoteFiles[x].localParent, gMissingRemoteFiles[x].remoteParent);
  173.       }
  174.  
  175.       if (transferObj.cancel) {
  176.         return;
  177.       }
  178.     }
  179.  
  180.     for (var x = 0; x < gDifferentFiles.length; ++x) {
  181.       if (gDifferentFiles[x].action != "nothing") {
  182.         transferObj.start(gDifferentFiles[x].action == "download",
  183.                           gDifferentFiles[x].action == "download" ? gDifferentFiles[x].remoteFile : gDifferentFiles[x].localFile,
  184.                           gDifferentFiles[x].localParent, gDifferentFiles[x].remoteParent);
  185.       }
  186.  
  187.       if (transferObj.cancel) {
  188.         return;
  189.       }
  190.     }
  191.  
  192.     for (var x = 0; x < gNewerFiles.length; ++x) {
  193.       if (gNewerFiles[x].action != "nothing") {
  194.         transferObj.start(gNewerFiles[x].action == "download",
  195.                           gNewerFiles[x].action == "download" ? gNewerFiles[x].remoteFile : gNewerFiles[x].localFile,
  196.                           gNewerFiles[x].localParent, gNewerFiles[x].remoteParent);
  197.       }
  198.  
  199.       if (transferObj.cancel) {
  200.         return;
  201.       }
  202.     }
  203.  
  204.     for (var x = 0; x < gOlderFiles.length; ++x) {
  205.       if (gOlderFiles[x].action != "nothing") {
  206.         transferObj.start(gOlderFiles[x].action == "download",
  207.                           gOlderFiles[x].action == "download" ? gOlderFiles[x].remoteFile : gOlderFiles[x].localFile,
  208.                           gOlderFiles[x].localParent, gOlderFiles[x].remoteParent);
  209.       }
  210.  
  211.       if (transferObj.cancel) {
  212.         return;
  213.       }
  214.     }
  215.   }
  216. }
  217.